//
//  DataBaseManager.m
//  kr_36
//
//  Created by 抠泥ser on 16/2/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//


#import "DataBaseManager.h"

@implementation DataBaseManager

/// 创建数据库方法
+ (DataBaseManager *)shareInstance
{
    // 设置静态指针变量 保证指针只有一个
    static DataBaseManager *dbManager = nil;
    // 判断是否是第一次调用该方法 指针为空 创建对象 指针不为空 返回
    if (!dbManager) {
        dbManager = [[DataBaseManager alloc] init];
    }
    return dbManager;
}

/// 打开数据库
- (void)openDB
{
    // 数据库路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"company.db"];
    // 创建数据库文件
    // 判断dbPoint指针中 是否存在数据库指针 如果不存在 再创建
    // 参数1: 数据库路径 UTF8转码
    // 参数2: 数据库指针地址
    int result = sqlite3_open(path.UTF8String, &dbPoint);
    NSLog(@"打开数据库: %d, 路径: %@", result, path);
}

/// 创建浏览表
- (void)createCollectDb
{
    // 1.SQL语句
    NSString *creatSQL = @"CREATE TABLE IF NOT EXISTS collect(title text, feedId text)";
    int result = sqlite3_exec(dbPoint, creatSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"创建表"];
}

/// 删除浏览表
- (void)dropCollectDb
{
    // SQL语句
    NSString *dropSQL = @"DROP TABLE collect";
    
    int result = sqlite3_exec(dbPoint, dropSQL.UTF8String, NULL, NULL, NULL);
    
    // 执行
    [self judgeResult:result message:dropSQL];
}

/// 添加浏览数据
- (void)insertCollect:(BrowesModel *)m
{
    // SQL 语句
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO collect(title, feedId) values('%@', '%@')", m.title, m.feedId];
    int result = sqlite3_exec(dbPoint, insertSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"添加数据"];
}


/// 删除浏览数据
- (void)deleteCollectId:(BrowesModel *)m
{
    NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM collect WHERE feedId = '%@'", m.feedId];
    int result = sqlite3_exec(dbPoint, deleteSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"删除"];
    
}


/// 查询表
- (NSMutableArray *)selectCollect
{
    // 1.SQL语句
    NSString *selectSQL = @"SELECT * FROM collect";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, selectSQL.UTF8String, -1, &stmt, nil);
    NSMutableArray *arr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            const unsigned char *feedId = sqlite3_column_text(stmt, 1);
            
            BrowesModel *m = [[BrowesModel alloc] init];
            m.title = [NSString stringWithUTF8String:(const char*)title];
            m.feedId = [NSString stringWithUTF8String:(const char*)feedId];
            [arr addObject:m];
        }
    } else {
        NSLog(@"失败了%d", result);
    }
    return arr;
}
/// 判断方法
- (void)judgeResult:(int)r message:(NSString *)str
{
    if (r == SQLITE_OK) {
        NSLog(@"%@成功", str);
    } else {
        NSLog(@"%@失败: %d", str, r);
    }
}

/*-------------------*/

/// 创建浏览表
- (void)createBrowseDb
{
    // 1.SQL语句
    NSString *creatSQL = @"CREATE TABLE IF NOT EXISTS browse(title text, feedId text)";
    int result = sqlite3_exec(dbPoint, creatSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"创建表"];
}
/// 删除浏览表
- (void)dropBrowseDb
{
    // SQL语句
    NSString *dropSQL = @"DROP TABLE browse";
    
    int result = sqlite3_exec(dbPoint, dropSQL.UTF8String, NULL, NULL, NULL);
    
    // 执行
    [self judgeResult:result message:dropSQL];
}

/// 添加浏览数据
- (void)insertBrwose:(BrowesModel *)m
{
    // SQL 语句
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO browse(title, feedId) values('%@', '%@')", m.title, m.feedId];
    int result = sqlite3_exec(dbPoint, insertSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"添加数据"];
}

/// 删除浏览数据
- (void)deleteBrwoseId:(BrowesModel *)m
{
    NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM browse WHERE feedId = '%@'", m.feedId];
    int result = sqlite3_exec(dbPoint, deleteSQL.UTF8String, NULL, NULL, NULL);
    [self judgeResult:result message:@"删除"];
    
}
/// 查询表
- (NSMutableArray *)selectBrwose
{
    // 1.SQL语句
    NSString *selectSQL = @"SELECT * FROM browse";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, selectSQL.UTF8String, -1, &stmt, nil);
    NSMutableArray *arr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            const unsigned char *feedId = sqlite3_column_text(stmt, 1);
            
            BrowesModel *m = [[BrowesModel alloc] init];
            m.title = [NSString stringWithUTF8String:(const char*)title];
            m.feedId = [NSString stringWithUTF8String:(const char*)feedId];
            [arr addObject:m];
        }
    } else {
        NSLog(@"失败了%d", result);
    }
    return arr;
}

@end
